package engine.systems.motion.wave
{
	import engine.classes.enumerations.SystemPriority;
	import engine.systems.spatial.spatialOffset.SpatialOffsetData;
	import engine.systems.update.GameSystem;
	
	public class WaveSystem extends GameSystem
	{
		private const WAVE:String = "wave";
		
		private var data:SpatialOffsetData;
		private var waveData:WaveData;
		
		public function WaveSystem()
		{
			super(WaveNode, updateNode, nodeAdded, nodeRemoved);
			
			this.priority = SystemPriority.PRE_MOTION;
		}
		
		public function updateNode(node:WaveNode, time:Number):void
		{
			data = node.offset.get(WAVE);
			
			for each(waveData in node.wave.data)
			{
				waveData.radians += waveData.rate * time;
				data[waveData.property] = Math.sin(waveData.radians) * waveData.radius;
			}
		}
		
		private function nodeAdded(node:WaveNode):void
		{
			node.offset.add(WAVE, new SpatialOffsetData());
		}
		
		private function nodeRemoved(node:WaveNode):void
		{
			node.offset.remove(WAVE);
		}
	}
}